$(function(){
	layui.use(["layer","form"],function(){
		var layer=layui.layer;
		var form=layui.form;
		
		form.verify({
			username: function(value){
				var userName=$("#userName").val();
				var isrepeat=false;
				$.ajax({
					type: "GET",
					url: "/user/verification",
					async: false,
					data: {userName:userName,id:$("#id").val()},
					beforeSend: function(request) {
						request.setRequestHeader("token", getCookie('token'));//添加token
					},
					success: function (r) {
						console.log(r)
						console.log(r.code)
						if(r.code === 10101){
//							layer.msg("用户名已存在,请重新填写");
							$("#userName").val("");
							isrepeat=true;
						}
//						return "用户名已存在,请重新填写";
					},
					error: function(event, XMLHttpRequest, ajaxOptions, thrownError){
						layer.msg('校验失败,请重试'); 
						layer.close(index); 
						console.log(event);
						console.log(XMLHttpRequest);
						console.log(ajaxOptions);
						console.log(thrownError);
					}
				});
				if(isrepeat){
					return "用户名重复,请重新填写";
				}
		    }
		});
		
		form.on('submit(formDemos)', function(data){
			  var index=layer.load(3, { shade: [0.5] });
			  $.ajax({
	                type: "POST",
	                url: "/user/saveUser",
	                data: data.field,
	                beforeSend: function(request) {
		   	             request.setRequestHeader("token", getCookie('token'));//添加token
		   	        },
	                success: function (r) {
	                	if(r.code ===0){
	                		parent.layer.msg('保存成功!', {icon: 1, time: 1800});
	                		parent.$('#exampleTable').bootstrapTable('refresh');
	                		parent.layer.close(index);
	                		parent.layer.closeAll("iframe");
	                	}else{
	                		layer.msg('保存失败请重试!', {icon: 1, time: 1800});
	                	}
	                },
	                error: function(event, XMLHttpRequest, ajaxOptions, thrownError){
	                	layer.msg('保存失败请重试'); 
	                	layer.close(index); 
	                	console.log(event);
	                	console.log(XMLHttpRequest);
	                	console.log(ajaxOptions);
	                	console.log(thrownError);
	               	}
	            });
		      return false;
		  });
		
		
		/*$("#submit").on("click",function(){
			var index=layer.load(3, { shade: [0.5] });
			var name=$("#name").val();
			var userName=$("#userName").val();
			var email=$("#email").val();
			if(!name){
				layer.msg("姓名不能为空");
				layer.close(index);
				return;
			}
			if(!userName){
				layer.msg("用户名不能为空");
				layer.close(index);
				return;
			}else{
				$.ajax({
					type: "GET",
					url: "/user/verification",
					data: {username:userName},
					beforeSend: function(request) {
						request.setRequestHeader("token", getCookie('token'));//添加token
					},
					success: function (r) {
						console.log(r)
						console.log(r.code)
						if(r.code === 10101){
							layer.msg("用户名已存在,请重新填写");
							$("#userName").val("");
							layer.close(index);
							return;
						}
					},
					error: function(event, XMLHttpRequest, ajaxOptions, thrownError){
						layer.msg('校验失败,请重试'); 
						layer.close(index); 
						console.log(event);
						console.log(XMLHttpRequest);
						console.log(ajaxOptions);
						console.log(thrownError);
					}
				});
			}
			var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"); //正则表达式
			if(!email){
				layer.msg("邮箱不能为空");
				layer.close(index);
				return;
			}
			if(!reg.test(email)){
				layer.msg("邮箱格式不正确");
				layer.close(index);
				return;
			}
			$.ajax({
                type: "POST",
                url: "/user/saveUser",
                data: $("#userform").serialize(),
                beforeSend: function(request) {
	   	             request.setRequestHeader("token", getCookie('token'));//添加token
	   	        },
                success: function (r) {
                	if(r.code ===0){
                		parent.layer.msg('保存成功!', {icon: 1, time: 1800});
                		parent.layer.close(index);
                		parent.layer.closeAll("iframe");
                	}else{
                		layer.msg('保存失败请重试!', {icon: 1, time: 1800});
                	}
                },
                error: function(event, XMLHttpRequest, ajaxOptions, thrownError){
                	layer.msg('保存失败请重试'); 
                	layer.close(index); 
                	console.log(event);
                	console.log(XMLHttpRequest);
                	console.log(ajaxOptions);
                	console.log(thrownError);
               	}
            });
		})*/
	})
})
